<?php

namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\System\UserRechargeFormRequest;
use App\Http\Requests\Admin\System\UserSaveFormRequest;
use App\Http\Requests\Admin\System\UserSetPasswordFormRequest;
use App\Repositories\Job\TagRepository;
use App\Repositories\System\RoleRepository;
use App\Repositories\System\UserRepository;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    public function __construct()
    {
        $this->set('system/user');
    }


    function index()
    {
        return $this->view('index');
    }

    function pager(UserRepository $rep, Request $request)
    {
        return $rep->adminPager($request->all());
    }

    function create()
    {
        return $this->view('create');
    }

    function save(UserSaveFormRequest $request, UserRepository $repository)
    {
        $data = $request->all();
        return $repository->save($data);
    }

    function update(UserSaveFormRequest $request, UserRepository $repository)
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $repository->save($data, $id);
    }

    function edit($id, UserRepository $repository)
    {
        $result = $repository->editInfo($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function batchDelete(Request $request, UserRepository $repository)
    {
        return $repository->batchDelete($request->post('ids', ''));
    }

    function delete(Request $request, UserRepository $repository)
    {
        return $repository->delete($request->post('id', ''));
    }

    function status($status, $id, UserRepository $repository)
    {
        return $repository->changeStatus($id, $status);
    }

    function setPasswordView($id)
    {
        return $this->view('setPassword')->with('id', $id);
    }

    function setPassword(UserSetPasswordFormRequest $request, UserRepository $repository)
    {
        $id = $request->get('id', 0);
        $password = $request->get('password', 0);
        return $repository->setPassword($id, $password);
    }

    function rechargeView($id)
    {
        return $this->view('recharge')->with('id', $id);
    }

    function recharge(UserRechargeFormRequest $request, UserRepository $repository)
    {
        $id = $request->get('id', 0);
        $money = $request->get('money', 0);
        return $repository->recharge($id, $money);
    }


    function choose_view(Request $request)
    {
        $choose_mode = $request->get('choose_mode', 1);
        $search = $request->all();
        return $this->view('choose', compact('choose_mode', 'search'));
    }

    function choose(UserRepository $rep, Request $request)
    {
        return $rep->choosePager($request->all());
    }
}
